<?php
/**
 * help_is_exist  判断帮忙是否存在
 * @param $help_id
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function help_is_exist($help_id)
{
    $help = D('Help/Help')->getHelp($help_id);
    if ($help) {
        return true;
    } else {
        return false;
    }
}

/**
 * check_is_sign  判断用户是否报名
 * @param $help_id
 * @param int $uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_sign($help_id, $uid = 0)
{
    $uid = $uid == 0 ? is_login() : $uid;
    $res = D('Help/HelpAttend')->where(array('help_id' => $help_id, 'uid' => $uid, 'status' => 0))->find();
    if ($res) {
        return true;
    } else {
        return false;
    }
}

/**
 * check_is_accept  判断用户是否被接受报名
 * @param $help_id
 * @param int $uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_accept($help_id, $uid = 0)
{
    $uid = $uid == 0 ? is_login() : $uid;
    $res = D('Help/HelpAttend')->where(array('help_id' => $help_id, 'uid' => $uid, 'status' => 1))->find();
    if ($res) {
        return true;
    } else {
        return false;
    }
}

/**
 * check_can_edit   判断用户是否有权限编辑
 * @param $id
 * @param int $uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_can_edit($id, $uid = 0)
{
    $uid = $uid == 0 ? is_login() : $uid;
    $help_creator = D('Help/Help')->where(array('id' => $id))->getField('uid');
    if ($help_creator == $uid || is_administrator()) {
        return true;
    }
    return false;
}

/**
 * check_is_my_publish  判断是否是用户发布的
 * @param $id
 * @param int $uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_my_publish($id, $uid = 0)
{
    $uid = $uid == 0 ? is_login() : $uid;
    $help_creator = D('Help/Help')->where(array('id' => $id))->getField('uid');
    if ($help_creator == $uid) {
        return true;
    }
    return false;
}

/**
 * get_type  获取帮忙类型
 * @param $id
 * @return mixed
 * @author:xjw129xjt xjt@ourstu.com
 */
function get_type($id)
{
    $type = D('Help/HelpType')->where(array('id' => $id, 'status' => 1))->getField('title');
    return $type;
}

/**
 * get_help_condition  获取帮忙状态
 * @param $condition
 * @return string
 * @author:xjw129xjt xjt@ourstu.com
 */
function get_help_condition($condition)
{
    switch ($condition) {
        case 1:
            $name = '<span style="color:green;">报名中</span>';
            break;
        case 2:
            $name = '<span style="color:green;">选人中</span>';
            break;
        case 3:
            $name = '<span style="color:green;">进行中</span>';
            break;
        case 4:
            $name = '<span style="color: purple;">已结束</span>';
            break;
        case 5:
            $name = '<span style="color: #f36119;">成功待评</span>';
            break;
        case 6:
            $name = '<span style="color: #f36119;">成功待作品</span>';
            break;
        case 7:
            $name = '<span style="color: #f36119;">完美成功</span>';
            break;
        case 0:
            $name = '<span style="color: #ff0000;">已失败</span>';
            break;
    }
    return $name;
}

/**
 * replace_0  替换0为空
 * @param $str
 * @return string
 * @author:xjw129xjt xjt@ourstu.com
 */
function replace_0($str)
{
    if (intval($str) == 0) {
        return '';
    }
    return $str;
}

/**
 * check_is_complain  检查是否已经投诉
 * @param $help_id
 * @param int $uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_complain($help_id, $uid = 0)
{
    $uid = $uid == 0 ? is_login() : $uid;
    $complain = D('Help/HelpComplain')->getComplain(array('help_id' => $help_id, 'uid' => $uid));
    if ($complain) {
        return true;
    }
    return false;
}

/**
 * check_is_evaluate  检查是否已经评价
 * @param $help_id
 * @param int $uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_evaluate($help_id, $uid = 0)
{
    $uid = $uid == 0 ? is_login() : $uid;
    $evaluate = D('Help/HelpEvaluate')->getEvaluate(array('help_id' => $help_id, 'uid' => $uid));
    if ($evaluate) {
        return true;
    }
    return false;
}

/**
 * check_publisher_is_evaluate  检查帮忙发布者是否已经评价
 * @param $help_id
 * @param int $uid
 * @param int $to_uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_publisher_is_evaluate($help_id, $uid = 0, $to_uid = 0)
{
    $evaluate = D('Help/HelpEvaluate')->getEvaluate(array('help_id' => $help_id, 'uid' => $uid, 'to_uid' => $to_uid));
    if ($evaluate) {
        return true;
    }
    return false;
}

/**
 * get_evaluate_type  获取评价类型
 * @param $type
 * @return string
 * @author:xjw129xjt xjt@ourstu.com
 */
function get_evaluate_type($type)
{
    switch ($type) {
        case 1:
            $name = '<i class="icon icon-good"></i>&nbsp;好评';
            break;
        case 2:
            $name = '<i class="icon icon-middle"></i>&nbsp;中评';
            break;
        case 3:
            $name = '<i class="icon icon-bad"></i>&nbsp;差评';
            break;
    }
    return $name;
}

/**
 * check_is_ask  检查是否已经求帮忙
 * @param $help_id
 * @param int $uid
 * @param int $to_uid
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_ask($help_id, $uid = 0, $to_uid = 0)
{
    $ask = D('Help/HelpAsk')->getAsk(array('help_id' => $help_id, 'uid' => $uid, 'to_uid' => $to_uid));
    if ($ask) {
        return true;
    }
    return false;
}

/**
 * check_sign_is_full  检查帮忙报名人数是否符合要求
 * @param $help_id
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_sign_is_full($help_id)
{
    $help_number = D('Help/Help')->where(array('id' => $help_id))->getField('number');
    $count = D('Help/HelpAttend')->getAllCountByHelpId($help_id);
    if ($count >= $help_number) {
        return true;
    }
    return false;
}

/**
 * check_publisher_is_chose  检查帮忙发布者是否已经选择帮手
 * @param $help_id
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_publisher_is_chose($help_id)
{
    $accept = D('Help/HelpAttend')->getAcceptByHelpId($help_id);
    if ($accept) {
        return true;
    }
    return false;
}

/**
 * check_is_all_evaluate  检查帮忙是否都已经评价
 * @param $help_id
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_all_evaluate($help_id)
{
    $accept = D('Help/HelpAttend')->getAcceptCount($help_id);
    $evaluate = D('Help/HelpEvaluate')->getEvaluateCount(array('help_id' => $help_id));
    if ($accept * 2 <= $evaluate) {
        return true;
    }
    return false;

}

/**
 * check_is_publish_work  检查是否已经发布作品
 * @param $help_id
 * @return bool
 * @author:xjw129xjt xjt@ourstu.com
 */
function check_is_publish_work($help_id)
{
    $work = D('Help/HelpWorks')->where(array('help_id'=>$help_id))->find();
    if($work){
        return true;
    }
    return false;
}

//zzl start
function get_address($province,$city,$district){
    $address='';
    if($province){
        $data=D('District')->where(array('id'=>$province))->field('name')->find();
        $address=$data['name'];
    }
    if($city){
        $data=D('District')->where(array('id'=>$city))->field('name')->find();
        $address.='-'.$data['name'];
    }
    if($district){
        $data=D('District')->where(array('id'=>$district))->field('name')->find();
        $address.='-'.$data['name'];
    }
    return $address;
}

//职业
function get_professional($type){
    $professional=D('Help/HelpType')->where(array('id'=>$type))->field('title')->find();
    return $professional['title'];
}


function check_attend_support($attend_id,$support = 1){
    $check = D('HelpAttendSupport')->where(array('attend_id' => $attend_id, 'uid' => is_login(),'type'=>$support))->select();
    if($check){
        return true;
    }
    return false;
}

function send_help_message($data=array()){
    if($data['to_uid']){
        $to_uid=$data['to_uid'];
    }else{
        $to_uid=D('Help/Help')->getUidById($data['to_help_id']);
    }
    $userInfo=query_user(array('nickname'),$data['from_uid']);
    $data['appName']=$data['appName']?$data['appName']:null;
    $data['appType']=$data['appType']?$data['appType']:null;
    sendMessage($to_uid,$userInfo['nickname'].$data['content'],$data['title'],U('help/index/detail',array('id'=>$data['to_help_id'])),$data['from_uid'],1,$data['appName'],$data['appType']);
    return true;
}

/**设为头条消息
 * @param $ids 帮忙ids
 * @return bool
 * @author 郑钟良<zzl@ourstu.com>
 */
function send_top_message($ids){
    foreach($ids as $val){
        $to_uid=D('Help/Help')->getUidById($val);
        sendMessage($to_uid,'管理员把您的帮忙设为了头条','帮忙：帮忙被设为头条',U('help/index/detail',array('id'=>$val)));
    }
    return true;
}

/**
 * @param $id
 * @return string
 * @author 郑钟良<zzl@ourstu.com>
 */
function type_title($id){
    if($id==0){
        $type['title']='其他';
    }else{
        $type=D('Help/HelpType')->where('id='.$id)->find();
    }
    return $type['title'];
}